#!/usr/bin/php
<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require("bashcommon.php");
$logid = startlog("uplist");
$starttime=mtime();
$blog =& LoggerManager::getLogger('bash_updatelist');

$blog->info("Started");
$_log->info("Started");
/* GET ACTIVE PROJECT LIST */
$prj = new project;
$prj->status=STATUS_ACTIVE;
$prjlist = $loader->loadListBy($prj,$prj->listByStatus(true));

$notification = array();

foreach ($prjlist as $inprj) {
	$totalprojects+=1;
	$blog->debug("============= PROJECT ".$inprj->id." - ".$inprj->name." =============");
	
	/* GET ACTIVE SUBPROJECT LIST */
	$sprj = new project;
	$sprj->status=STATUS_ACTIVE;
	$sprj->upd="true";
	$sprj->parent=$inprj->id;
	$sprjlist = $loader->loadListBy($sprj,$sprj->listByAny());	

	foreach ($sprjlist as $insub) {
		$blog->debug("============= SUBPROJECT ".$insub->id." - ".$insub->name." =============");
		/* GET ACTIVE TEAM LIST */
		$team = new team;
		$teamlist = $loader->loadListBy($team,$team->listByReady($inprj->id,$insub->id,true));

		foreach ($teamlist as $inteam) {
			$blog->debug("============= TEAM ".$inteam->id." - ".$inteam->name. " =============");
			$prj = $inprj;
			$subprj = $insub;
			$team = $inteam;
			$update=true;
			include(ROOT_PATH."includes/setupfilescreator.php");
			if (count($deletefiles)>0) {
				addNotificationFile($notification,$inteam->id,$deletefiles,$inprj->name,$insub->name,"rm");
			}
			if (count($newfiles)>0) {
				addNotificationFile($notification,$inteam->id,$newfiles,$inprj->name,$insub->name,"add");
			}			
			$blog->debug("============= FINISHED TEAM ".$inteam->id." =============");
		}
		$blog->debug("============= FINISHED SUBPROJECT ".$insub->id." =============");	
	}

	$blog->debug("============= FINISHED PROJECT ".$inprj->id." =============");

}

if (DISABLE_STATS_NOTIFY != "true") {
	notifyListUpdate($notification);
} else {
	$blog->info("Notification disabled");
	$_log->info("Notification disabled");
}

$endtime=mtime();
endlog($logid);
$db->close();
$blog->info("Finished");
$_log->info("Finished");
$blog->info("============= * =============");
$_log->info("============= * =============");
$statstext = "Projects processed: ".$totalprojects;
$blog->info($statstext);
$_log->info($statstext);
$blog->info("Process time: ".sec2hms($endtime-$starttime,true));
$_log->info("Process time: ".sec2hms($endtime-$starttime,true));
?>